package
{
	import flash.display.Sprite;
	import flash.events.*;
	
	public class CPU extends Sprite
	{		
		public static var running:Boolean = false;
		public static var pc:int = 0;
		
		private static var tickev:Event = new Event("TICK", true, false);
		
		public function CPU()
		{
			super();			
			addEventListener("TICK", tick, false, 1000);
		}
		
		public function tick(ev:Event):void {
			m1.debug.print("CPU cycle="+pc);
			pc++;
			try {
				if (running) dispatchEvent(tickev);
			} catch (error:Error) {
				m1.debug.print("recursion error?");
			}
			
		}
		
		public function start():void {			
			pc=0;
			m1.debug.print("CPU started!");
			running = true;
			try {
				if (running) dispatchEvent(tickev);
			} catch (error:Error) {
				m1.debug.print("recursion error?");
			}
		}
		
		public function stop():void {
			m1.debug.print("CPU stopped!");
			running = false;
		}
		
		public function probe():void{
			m1.debug.print("has listener="+hasEventListener("TICK"));
		}
		
	}
}